//app.js
App({
  data: {
    //serverIp: 'http://47.102.37.139',
    //serverIp: 'http://localhost',
    serverIp: 'https://www.pengfa.online',
    //serverIp: 'http://10.71.246.229',
    serverPort: ':8081',
    sessionId: '',
    userRoleList: null
  },
  //imgBase: "http://localhost:8081",
  imgBase: "https://www.pengfa.online:8081",
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    var that = this;
    wx.login({
      success(res) {
        if (res.code) {
          wx.request({
            url: that.data.serverIp + that.data.serverPort + '/wxUser/getWXUserInfo',
            data: {
              code: res.code
            },
            success(res) {
              wx.hideLoading()
              if (res.data == null) {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              } else {
                that.data.sessionId = res.data.data.key;
                that.data.userRoleList = res.data.data.userInfo.roleId;
                var userRoleList = wx.getStorageSync('userRoleList') || []
                userRoleList = that.data.userRoleList;
                wx.setStorageSync('userRoleList', userRoleList)
                if (that.data.userRoleList.length > 1) {
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }else{
                  wx.redirectTo({
                    url: '/pages/home/home'
                  })
                }
              }
            },
            fail(res) {
              wx.hideLoading()
              wx.showToast({
                title: '登陆失败',
                icon: 'none'
              })
            }
          })
        } else {
          wx.hideLoading()
          wx.showToast({
            title: '登陆失败',
            icon: 'none'
          })
        }
      }
    })
  },
})